package cn.gson.springboot.model.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.DrugApplyDao;
import cn.gson.springboot.model.dao.DrugDao;
import cn.gson.springboot.model.dao.DrugapplyDetailDao;
import cn.gson.springboot.model.mapper.DrugApplyMapper;
import cn.gson.springboot.model.pojos.ClinicCard;
import cn.gson.springboot.model.pojos.Drug;
import cn.gson.springboot.model.pojos.DrugApply;
import cn.gson.springboot.model.pojos.DrugapplyDetail;
import cn.gson.springboot.model.vo.DrugqlVo;



@Service
@Transactional(rollbackOn=Exception.class)
public class DrugApplyService {

	@Autowired
	private DrugApplyMapper qlmapper;
	
	
	//获取药品
	@Autowired
	private DrugDao drugdao;
	
	//请领表
	@Autowired
	private DrugApplyDao drugapplydao;
	
	//请领详情
	@Autowired
	private DrugapplyDetailDao drugapplydetaildao;
	//查询所有门诊库存
		public JSONArray selectAllqljl() {
			return qlmapper.selectAllqljl();
		}
	
	//根据请领查询或者查询所有
		public JSONArray  selectAllqlidkaid(Integer drugapplyid) {
			return qlmapper.selectAllqlidkaid(drugapplyid);
		}
		
	//新请领记录
		public void DrugIndent(DrugqlVo drugqlVo) {
			//获取药品信息进行新增
			DrugApply drugapply = drugqlVo.getDrugapply();
			
			DrugApply save = drugapplydao.save(drugapply);
			
			List<DrugapplyDetail> details = drugqlVo.getDrugapplyDetail();
			
			//循环
			for (DrugapplyDetail pur : details) {
			//添加药品信息
			pur.setDrugApply(drugapply);
			//查询药品
			Drug drug = drugdao.findById(pur.getDrug().getDrugNumber()).get();
			//重新赋值药品对象
			pur.setDrug(drug);
			drugapplydetaildao.save(pur);
			}
		}
		
}
